package com.lineage.server.serverpackets;

import com.lineage.server.datatables.ItemSpecialAttributeTable;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.templates.L1ItemSpecialAttribute;
import com.lineage.server.templates.L1ItemSpecialAttributeChar;

public class S_IdentifyDesc extends ServerBasePacket {
  private byte[] _byte;
  
  public S_IdentifyDesc(L1ItemInstance item) {
    this._byte = null;
    buildPacket(item);
  }
  
  private void buildPacket(L1ItemInstance item) {
    L1ItemSpecialAttributeChar attr_char;
    writeC(245);
    writeH(item.getItem().getItemDescId());
    StringBuilder name = new StringBuilder();
    switch (item.getItem().getBless()) {
      case 0:
        name.append("$227 ");
        break;
      case 2:
        name.append("$228 ");
        break;
    } 
    name.append(item.getItem().getNameId());
    switch (item.getItem().getType2()) {
      case 1:
        writeH(134);
        writeC(3);
        writeS(name.toString());
        attr_char = item.get_ItemAttrName();
        if (attr_char != null) {
          L1ItemSpecialAttribute attr = ItemSpecialAttributeTable.get().getAttrId(attr_char.get_attr_id());
          writeS(String.valueOf(String.valueOf(item.getItem().getDmgSmall() + attr.get_dmg_small())) + "+" + 
              item.getEnchantLevel());
          writeS(String.valueOf(String.valueOf(item.getItem().getDmgLarge() + attr.get_dmg_large())) + "+" + 
              item.getEnchantLevel());
          break;
        } 
        writeS(String.valueOf(String.valueOf(item.getItem().getDmgSmall())) + "+" + item.getEnchantLevel());
        writeS(String.valueOf(String.valueOf(item.getItem().getDmgLarge())) + "+" + item.getEnchantLevel());
        break;
      case 2:
        item.getItem().getItemId();
        writeH(135);
        writeC(2);
        writeS(name.toString());
        writeS(String.valueOf(String.valueOf(Math.abs(item.getItem().get_ac()))) + "+" + item.getEnchantLevel());
        break;
      case 0:
        switch (item.getItem().getType()) {
          case 1:
            writeH(137);
            writeC(3);
            writeS(name.toString());
            writeS(String.valueOf(item.getChargeCount()));
            break;
          case 2:
            writeH(138);
            writeC(2);
            name.append(": $231 ");
            name.append(String.valueOf(item.getRemainingTime()));
            writeS(name.toString());
            break;
          case 7:
            writeH(136);
            writeC(3);
            writeS(name.toString());
            writeS(String.valueOf(item.getItem().getFoodVolume()));
            break;
          default:
            writeH(138);
            writeC(2);
            writeS(name.toString());
            break;
        } 
        writeS(String.valueOf(item.getWeight()));
        break;
    } 
  }
  
  public S_IdentifyDesc() {
    this._byte = null;
    L1ItemInstance item = ItemTable.get().createItem(2);
    writeC(245);
    writeH(item.getItem().getItemDescId());
    writeH(134);
    writeC(3);
    writeS(item.getName());
    writeS(String.valueOf(String.valueOf(item.getItem().getDmgSmall())) + "+" + item.getEnchantLevel());
    writeS(String.valueOf(String.valueOf(item.getItem().getDmgLarge())) + "+" + item.getEnchantLevel());
    writeS(String.valueOf(item.getWeight()));
  }
  
  public byte[] getContent() {
    if (this._byte == null)
      this._byte = getBytes(); 
    return this._byte;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
